package com.youaishujuhui.devops.web.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.domain.manage.SysResource;
import com.youaishujuhui.devops.web.service.manage.SysResourceService;
import com.youaishujuhui.devops.web.vo.Page;
import com.youaishujuhui.devops.web.vo.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  资源管理

 **/
@Controller
@RequestMapping("/manage/resource")
public class SysResourceController {

    @Autowired
    private SysResourceService sysResourceService;

    @RequestMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("manage/resource");

    }

    @RequestMapping("/query")
    @ResponseBody
    public Result<List<SysResource>> query(Integer limit,Integer offset,String sort,String order ){
        Map<String,Object> param = new HashMap<>();
        param.put("limit",limit);
        param.put("offset",offset);
        param.put("sort",sort);
        param.put("order",order);
        Result<List<SysResource>> result = new Result<>();
        int total = sysResourceService.total();

        List<SysResource> list = sysResourceService.query(param);
        Page page = new Page(total,limit);
        result.setPage(page);
        result.setData(list);
        result.setCode(Result.SUCCESS);

        //System.out.println(JSON.toJSONString(result));

        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result<String> add(SysResource po){
        Result<String> result = new Result<>();
        int count = sysResourceService.countById(po);
        if (count > 0){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("添加失败，资源ID \"%s\" 已存在",po.getResId()));
        }else{
            result.setCode(Result.SUCCESS);
            result.setMsg("添加成功");
            sysResourceService.insert(po);
//cacheManager.loadResourceUri();
        }

        return result;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Result<SysResource> findById(Integer id){
        Result<SysResource> result = new Result<>();
        SysResource po = sysResourceService.findById(id);
        if(po == null){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("记录已不存在！"));
        } else {
            result.setCode(Result.SUCCESS);
            result.setData(po);
        }

        return result;
    }
    
    @RequestMapping("/edit")
    @ResponseBody
    public Result<String> edit(SysResource po){
        Result<String> result = new Result<>();
        sysResourceService.update(po);
//cacheManager.loadResourceUri();
        result.setCode(Result.SUCCESS);
        result.setMsg("修改成功！");
        return result;
    }

    @RequestMapping("/del")
    @ResponseBody
    public Result<String> delete(Integer id){
        Result<String> result = new Result<>();
        sysResourceService.deleteById(id);
//cacheManager.loadResourceUri();
        result.setCode(Result.SUCCESS);
        result.setMsg("删除成功！");
        return result;
    }
}
